package net.jiastudy.biz.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import net.jiastudy.biz.sys.entity.File;
import net.jiastudy.biz.sys.mapper.FileMapper;
import net.jiastudy.biz.sys.service.FileService;
import net.jiastudy.core.base.BaseService;
import net.jiastudy.core.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional(rollbackFor = Exception.class)
public class FileServiceImpl extends BaseService<FileMapper, File> implements FileService {

    @Override
    public File create(File file) {
        if (this.insert(file)) {
            return file;
        }
        return null;
    }

    @Override
    public String getFilePreviewPath(Long fileId, StringBuffer host) {
        File file = get(fileId);
        if (null == file) {
            throw new ServiceException("该文件不存在！");
        }

        Integer convertType = null;
        if (file.getExtName().equals(".doc")
                || file.getExtName().equals(".docx")
                || file.getExtName().equals(".xls")
                || file.getExtName().equals(".xlsx")
                || file.getExtName().equals(".ppt")
                || file.getExtName().equals(".pptx")
                || file.getExtName().equals(".rar")
                || file.getExtName().equals(".zip")
        ) {
            convertType = 0;
        } else if (file.getExtName().equals(".pdf")) {
            convertType = 14;
        } else if (
                file.getExtName().equals(".png")
                        || file.getExtName().equals(".bmp")
                        || file.getExtName().equals(".jpg")
                        || file.getExtName().equals(".gif")
        ) {
            convertType = 23;
        }

        if (null != convertType) {
            String url = "http://doc.czie.edu.cn/dcs.web/onlinefile?convertType=" + convertType + "&downloadUrl=" + host + file.getPath();
            String data = HttpUtil.get(url);
            JSONObject dataJson = JSONObject.parseObject(data);
            if (!dataJson.isEmpty() && dataJson.containsKey("data")){
                String path = dataJson.get("data").toString();
                StrUtil.removeAll(path, "[");
                StrUtil.removeAll(path, "]");

                return path;
            }
        }

        throw new ServiceException("该文件不支持预览！");
    }

    @Override
    public boolean delete(Long id) {
        // TODO 物理删除
        return this.delete(id);
    }

    @Override
    public boolean delete(Set<Long> ids) {
        // TODO 物理删除
        return this.deleteBatchIds(ids);
    }

    @Override
    public File get(Long id) {
        return this.selectById(id);
    }

    @Override
    public List<File> findByIds(Set<Long> ids) {
        if (null == ids || ids.size() == 0) {
            return null;
        }
        return this.selectBatchIds(ids);
    }
}
